package com.lychee.moli.controller;

import com.lychee.moli.entity.BaseResultEntity;
import com.lychee.moli.entity.IdentityEntity;
import com.lychee.moli.entity.UserInfoEntity;
import com.lychee.moli.service.IUserService;
import com.lychee.moli.utils.TokenUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Lychee on 2018/4/9 10:34.
 * <p>
 * Author:Hejingzhou
 * <p>
 * Email:tcowork@163.com
 */
@RequestMapping(value = "/User")
@Controller
public class UserController {

    @Resource
    private IUserService mUserService;
    private BaseResultEntity mRegisterSuccess;

    /**
     * 用户注册
     *
     * @param username            注册用户名
     * @param password            注册密码
     * @param sex                 性别
     * @param httpServletRequest
     * @param httpServletResponse
     */
    @RequestMapping(value = "/registerUser")
    @ResponseBody
    public BaseResultEntity insertIntoUser(@RequestParam(value = "userName") String username,
                                           @RequestParam(value = "password") String password,
                                           @RequestParam(value = "sex") String sex,
                                           HttpServletRequest httpServletRequest,
                                           HttpServletResponse httpServletResponse) throws IOException {

        System.out.println(httpServletRequest.getParameter("userName"));


        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setUserName(username);
        userInfoEntity.setPassword(password);
        userInfoEntity.setSex(sex);


        Logger logger = Logger.getLogger(getClass().getName());

        int primaryID = mUserService.registerUser(userInfoEntity);
        logger.info("插入用户返回的主键 " + primaryID);
        if (primaryID > 0) {
            mRegisterSuccess = new BaseResultEntity<UserInfoEntity>(BaseResultEntity.ERROR_CODE_SUCCESS, userInfoEntity, "注册成功");
            httpServletResponse.setContentType("text/html;charset=UTF-8");
            logger.info("注册新用户成功一次" + userInfoEntity.getId());
        } else {
            mRegisterSuccess = new BaseResultEntity<UserInfoEntity>(BaseResultEntity.ERROR_CODE_FAIL, null, "注册失败");
            logger.error("注册新用户失败一次");
        }
        return mRegisterSuccess;
    }

    /**
     * 用户登录
     *
     * @param userName 登录用户名
     * @param password 登录密码
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public BaseResultEntity login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        UserInfoEntity userInfo = mUserService.login(userName, password);
        if (userInfo != null) {
            IdentityEntity identityEntity = new IdentityEntity();
            identityEntity.setUserName(userName);
            identityEntity.setUserId(String.valueOf(userInfo.getId()));
            identityEntity.setIssuer("HeIssue");
            //设置有效期半年
            identityEntity.setDuration((long) (1000 * 60 * 60 * 31 * 6));
            String token = TokenUtils.createToken(identityEntity, TokenUtils.SECRET_KEY);
            return new BaseResultEntity<String>(BaseResultEntity.ERROR_CODE_SUCCESS, token, "登陆成功");
        }
        return new BaseResultEntity<String>(BaseResultEntity.ERROR_CODE_FAIL, null, "暂无注册，请您先注册");
    }
}
